package com.zeedev.flyingsaucer.beers.activities.login;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.zeedev.flyingsaucer.modules.StoreModule.interfaces.Store;
import mobi.zeedev.flyingsaucer.common.CredentialsModule.interfaces.ICredentials;
import mobi.zeedev.flyingsaucer.common.DataModule.tree.DatabaseTree;
import roboguice.inject.ContextScoped;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 * User: James Johnson
 * Date: Oct 28, 2010
 * Time: 4:21:13 PM
 */

@ContextScoped
public class LoginModel {

    private Boolean isNonMember = false;
    private Boolean isRefreshing = false;
    private Boolean isRememberMe = false;

    private String storeNumber = null;

    @Inject
    @Named("autoPreferenceCredentials")
    ICredentials credentials;

    PropertyChangeSupport pcs = new PropertyChangeSupport(this);

    private Store storeList;

    public static final String NON_MEMBER = "nonMember";
    public static final String RESFRESHING = "resfreshing";
    public static final String REMEMBER_ME = "rememberMe";
    public static final String STORE_LIST = "storeList";
    public static final String STORE_NUMBER = "storeNumber";

    @Inject
    public LoginModel(Store storeList) {
        this.storeList = storeList;

        pcs.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(STORE_NUMBER) && evt.getNewValue() != null) {
                    credentials.setStoreNumber(evt.getNewValue().toString());
                }
            }
        });
    }

    public Boolean isNonMember() {
        return isNonMember;
    }

    public String getStoreNumber() {
        return storeNumber;
    }

    public void setStoreNumber(String storeNumber) {
        String oldValue = this.storeNumber;

        this.storeNumber = storeNumber;
        pcs.firePropertyChange(STORE_NUMBER, oldValue, this.storeNumber);
    }

    public void setNonMember(Boolean nonMember) {
        Boolean oldValue = isNonMember;
        isNonMember = nonMember;
        pcs.firePropertyChange(NON_MEMBER, oldValue, nonMember);
    }

    public Boolean isRefreshing() {
        return isRefreshing;
    }

    public void setRefreshing(Boolean refreshing) {
        Boolean oldValue = isRefreshing;
        isRefreshing = refreshing;
        pcs.firePropertyChange(RESFRESHING, oldValue, refreshing);
    }

    public Boolean isRememberMe() {
        return isRememberMe;
    }

    public void setRememberMe(Boolean rememberMe) {
        Boolean oldValue = isRememberMe;
        isRememberMe = rememberMe;
        pcs.firePropertyChange(REMEMBER_ME, oldValue, rememberMe);
    }


    public Store getStoreList() {
        return storeList;
    }

    public void setStoreList(DatabaseTree tree) {

        this.storeList.setDatabaseTree(tree);

        pcs.firePropertyChange(STORE_LIST, storeList, storeList);

    }


}
